﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using bts_webapp.Models;

namespace bts_webapp
{
    public partial class UxUser : System.Web.UI.UserControl
    {

        public string UserId
        {
            get
            {
                return this.txtUserId.Text;
            }
            set {
                this.txtUserId.Text = value;
            }
        }

        public string Fullname
        {
            get
            {
                return this.txtFullname.Text;
            }
            set
            {
                this.txtFullname.Text = value;
            }
        }

        public string Password {
            get {
                return this.txtPassword.Text;
            }
            set
            {
                this.txtPassword.Text = value;
                this.txtPassword.Attributes.Add("value", value);
                //this.txtPassword.
            }
        }

        public string Address
        {
            get
            {
                return this.txtAddress.Text;
            }
            set
            {
                this.txtAddress.Text = value;
            }
        }

        public string Phone
        {
            get
            {
                return this.txtPhone.Text;
            }
            set
            {
                this.txtPhone.Text = value;
            }
        }

        public string Email
        {
            get
            {
                return this.txtEmail.Text;
            }
            set
            {
                this.txtEmail.Text = value;
            }
        }

        public string Role
        {
            get
            {
                return this.ddlRole.SelectedItem.Text;
            }
            set
            {
                if (value.ToLower() == "admin") {
                    this.ddlRole.SelectedValue = "0";
                }
                else if (value.ToLower() == "Nhân viên tổ ViBa")
                {
                    this.ddlRole.SelectedValue = "1";
                }
                else
                    this.ddlRole.SelectedValue = "2";
            }
        }

        public void SetFieldCode(bool enabled)
        {
            this.txtUserId.Enabled = enabled;
        }

        public bool Status
        {
            get
            {
                return this.chkStatus.Checked;
            }
            set
            {
                this.chkStatus.Checked = value;
            }
        }

        public bool InsertOrUpdate
        {
            set {
                this.btnUpdate.CommandName = value ? "PerformInsert" : "Update";
                this.btnUpdate.Text = value ? "Insert" : "Update";
            }
            
        }

        public void BindData(VIBA_User user)
        {
            this.UserId = user.User_Id;
            this.Password = user.User_Pass;
            this.Fullname = user.User_Full_Name;
            this.Address = user.User_Address;
            this.Phone = user.User_Phone;
            this.Email = user.User_Email;
            this.Status = user.User_Status == 1;
            this.Role = GetRole(user.User_Role.Value);
        }

        private string GetRole(int r)
        {
            if (r == 0)
                return "Admin";
            if (r == 1)
                return "Nhân viên tổ ViBa";
            return "Nhân viên các đài/trạm";
        }

        public void SetStatus(string message) {
            this.litStatus.Text = string.Format("<font color='red'>{0}</font>", message);
        }

        public VIBA_User GetData()
        {
            VIBA_User user = new VIBA_User 
            { 
                User_Id = this.UserId,
                User_Pass = this.Password,
                User_Full_Name = this.Fullname,
                User_Address = this.Address,
                User_Email = this.Email,
                User_Phone = this.Phone,
                User_Role = GetRole(this.Role),
                User_Status = GetStatus(this.Status)
            };

            return user;
        }

        private int GetStatus(bool status)
        {
            return this.chkStatus.Checked ? 1 : 0;
        }

        private int GetRole(string role)
        {
            switch (role.ToLower())
            { 
                case "admin":
                    return 0;
                case "Nhân viên tổ ViBa":
                    return 1;
                case "Nhân viên các đài/trạm":
                    return 2;
                default:
                    return 2;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            InitComboBoxRole();
        }

        private void InitComboBoxRole()
        {
            if (this.ddlRole.Items.Count > 0)
                return;

            this.ddlRole.Items.Clear();
            int[] roles = new int[] { 0, 1, 2 };
            string[] roleNames = new string[] { "Admin", "Nhân viên tổ ViBa", "Nhân viên các đài/trạm" };
            for (int i = 0; i < roles.Length; i++)
                ddlRole.Items.Add(new RadComboBoxItem(roleNames[i], roles[i].ToString()));
        }
    }
}